
/*
Do-file name: 6_AKM_estimation.do
Author: Thiago
Date: Nov 7th 2018
This version: April 2021
Comments: This do-file exports dataset for AKM estimation in Stata and produces all figures and tables
related to Concurso data.
*/

*use "$data_out/judgelevel_estimation_includeFE_deident.dta", clear
use "$data_out/judgelevel_estimation_includeFE.dta", clear

******************************************************************************
**** Figure 5: Binscatter of residual ranks, conditioning on Concurso FE *****
******************************************************************************

gen temp_fe = -judge_fe_std
reghdfe temp_fe  Rank_concurso, a(Concurso_enc)
loc b: di %-5.3f _b[Rank_concurso]
mat V =e(V)
loc sd: di %-5.3f sqrt(V[1,1])

binscatter judge_fe_std Rank_concurso, n(40) yscale(rev) ytitle("Residualized Fixed Effects") ///
	xtitle("Rank in Exam") absorb(Concurso_enc) ylab(, nogrid) graphregion(color(white)) ///
	text(-0.5 0 "{&beta} = `b' [`sd']")
graph export "$paper_figures/binscatter_grades_ihs_std.pdf", replace

******************************************************************************
*******   Figure 4:Binscatter of hearings and case disposition ***************
******************************************************************************

reg judge_fe_hear judge_fe, absorb(connected_sets)
loc b: di %-5.3f _b[judge_fe]
mat V =e(V)
loc sd: di %-5.3f sqrt(V[1,1])
loc r2: di %-5.2f e(r2)

binscatter judge_fe_hear judge_fe, xtitle("Decisions FE") ytitle("Hearings FE")  ///
	absorb(connected_sets) ylab(, nogrid) graphregion(color(white)) mcolor(dknavy%40) n(100) ///
	text( 1.5 -1.5 "R{sup:2} = `r2'") text( 1.2 -1.5 "{&beta} = `b' [`sd']") 
graph export "$paper_figures/scatter_hearings_decisions.pdf", replace

******************************************************************************
*Table 5: Main results – correlation between admission grades and performance*
******************************************************************************

eststo drop *

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std Final_grade_z i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std Final_grade_z i.Concurso_enc ///
	if (Objective_Exam_z !=. & Written_Exam_z != . & Essay_Exam_Civil_z!=. & Essay_Exam_Penal_z !=. & Oral_Exam_z!=. & Grade_Titles_z !=.), robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'
eststo: reg judge_fe_std Objective_Exam_z Written_Exam_z Essay_Exam_Civil_z Essay_Exam_Penal_z Oral_Exam_z Grade_Titles_z i.Concurso_enc if Final_grade_z!=., robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

esttab using "$paper_tables/regressions_quintiles.tex", booktabs  f drop(_cons *Concurso_enc* 5.quintile) replace ///
	stats(N r2  cluster concurso ,labels(Observations R-Squared "Number Admission Cohorts" ///
	"Concurso Fixed-Effect" )) label  nomtitles se r2 star(* 0.10 ** 0.05 *** 0.01)  ///
	coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile")

******************************************************************************
*Table A6: Pairwise correlations between performance in admission exam phases*
******************************************************************************
loc count = 0
foreach var of varlist Grade_Titles Essay_Exam_Civil Essay_Exam_Penal Objective_Exam Oral_Exam Written_Exam {
	reghdfe `var' if (judge_fe_std!=. & Final_grade_z!=.), a(Concurso_enc) res(r_`var')
}
lab var r_Objective_Exam "Objective"
lab var r_Written_Exam "Written" 
lab var r_Essay_Exam_Civil "Civil case"
lab var r_Essay_Exam_Penal "Criminal case" 
lab var r_Oral_Exam "Oral"
lab var r_Grade_Titles "Titles"

estpost correlate r_Objective_Exam r_Written_Exam r_Essay_Exam_Civil r_Essay_Exam_Penal  r_Oral_Exam r_Grade_Titles ///
	if (Objective_Exam_z !=. & Written_Exam_z != . & Essay_Exam_Civil_z!=. & Essay_Exam_Penal_z !=. & Oral_Exam_z!=. & Grade_Titles_z !=.), matrix

esttab . using "$paper_tables/correlation_grades.tex", ///
	not unstack booktabs nonum label f noobs nodepvars  star(* 0.10 ** 0.05 *** 0.01) replace 

******************************************************************************
***********  Table 6: Robustness – excluding top and bottom performers *******
******************************************************************************

bys Concurso_enc: egen number_cand = max(Rank_concurso)
gen threshold_min = round(0.05 * number_cand)

eststo drop *

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc, robust
estadd local top3 "No"
estadd local top5 "No"
estadd local bottom5 "No"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc if Rank_concurso > 3 , robust
estadd local top3 "Yes"
estadd local top5 "No"
estadd local bottom5 "No"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc if Rank_concurso > threshold_min  , robust
estadd local top3 "No"
estadd local top5 "Yes"
estadd local bottom5 "No"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc if Rank_concurso <(number_cand - threshold_min), robust
estadd local top3 "No"
estadd local top5 "No"
estadd local bottom5 "Yes"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc if (Rank_concurso > threshold_min & Rank_concurso < (number_cand - threshold_min)), robust
estadd local top3 "No"
estadd local top5 "Yes"
estadd local bottom5 "Yes"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

esttab using "$paper_tables/regressions_selected.tex", booktabs  f drop(_cons *Concurso_enc* 5.quintile) replace ///
	stats(N r2  cluster top3 top5 bottom5 , fmt(%12.0fc %12.3fc) labels(Observations R-Squared "Number Admission Cohorts" "Drop Top 3" ///
	"Drop Top 5\%" "Drop Bottom 5\%" "Drop Top \& Bottom 5\%" )) label  nomtitles se  ///
	star(* 0.10 ** 0.05 *** 0.01)  coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile")

******************************************************************************
* Table A4: Robustness – correlation between admission grades and performance*
******************************************************************************

eststo drop *

eststo: reg Rank_FE_IS ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg Rank_FE_IS Final_grade_z i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg Rank_FE_IS Final_grade_z i.Concurso_enc if (Objective_Exam_z !=. & Written_Exam_z != . & Essay_Exam_Civil_z!=. & Essay_Exam_Penal_z !=. & Oral_Exam_z!=. & Grade_Titles_z !=.)  , robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg Rank_FE_IS Objective_Exam_z Written_Exam_z Essay_Exam_Civil_z Essay_Exam_Penal_z Oral_Exam_z Grade_Titles_z i.Concurso_enc if Final_grade_z!=., robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

esttab using "$paper_tables/regressions_quintiles_rank.tex", booktabs  f drop(_cons *Concurso_enc* 5.quintile) replace ///
	stats(N r2  cluster concurso ,labels(Observations R-Squared "Number Admission Cohorts" ///
	"Concurso Fixed-Effect" )) label  nomtitles se r2 star(* 0.10 ** 0.05 *** 0.01)  ///
	coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile")

******************************************************************************
******** Table A7: Relationship between promotion and performance   **********
******************************************************************************

eststo drop *

eststo: reghdfe promotion_max_single ib5.quintile            , a(Concurso_enc) vce(robust)
estadd local concurso "Yes"
estadd local modelo "Promotion"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'
qui summ promotion_max_single if e(sample)==1
loc mean: di %6.3fc `r(mean)'
estadd local mean `mean'

eststo: reghdfe promotion_max_single judge_fe_std            , a(Concurso_enc) vce(robust)
estadd local concurso "Yes"
estadd local modelo "Promotion"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'
qui summ promotion_max_single if e(sample)==1
loc mean: di %6.3fc `r(mean)'
estadd local mean `mean'

eststo: reghdfe promotion_max_single ib5.quintile judge_fe_std , a(Concurso_enc) vce(robust)
estadd local concurso "Yes"
estadd local modelo "Promotion"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'
qui summ promotion_max_single if e(sample)==1
loc mean: di %6.3fc `r(mean)'
estadd local mean `mean'

esttab using "$paper_tables/regressions_promotions.tex", booktabs  f drop(_cons 5.quintile) replace ///
	stats(N r2 cluster mean   , fmt(%12.0fc %12.3fc %12.3fc) labels(Observations R-Squared "Number Admission Cohorts" ///
	"Dep Var Mean" )) label  nomtitles se r2  ///
	coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile" judge_fe_std "Judge FE") star(* 0.10 ** 0.05 *** 0.01) 

	
******************************************************************************
********   Table 7: Robustness – alternative sample restrictions    **********
******************************************************************************

eststo drop *

eststo: qui reg judge_fe_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Baseline"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: qui reg judge_fe_full_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Full sample"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: qui reg judge_fe_spell4_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "4-month spells"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: qui reg judge_fe_total6_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "6-month total"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'	 

eststo: qui reg judge_fe_first2_std  ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Drop first 2 months"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: qui reg judge_fe_firstlast2_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Drop first \& last 2 months"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'  

eststo: qui reg judge_fe_bigcourt_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Large courts"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'  

esttab using "$paper_tables/regressions_quintiles_samples_robustness.tex", booktabs  f drop(_cons *Concurso_enc* 5.quintile) replace ///
	stats(N r2  cluster concurso modelo ,fmt(%12.0fc %12.3fc %12.0fc) labels(Observations R-Squared "Number Admission Cohorts" ///
	"Concurso Fixed-Effect" "Sample restriction" )) label  nomtitles se r2  ///
	coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile") star(* 0.10 ** 0.05 *** 0.01) 

******************************************************************************
* Table A13: Correlation between judges' fixed-effects and individual characteristics 
******************************************************************************

bys connected_sets: egen FE_sd_cs = sd(judge_fe)
gen FE_IHS_cs = judge_fe / FE_sd_cs

global trait "gender_RAIS age_2015  age_2015_sqr educ_grad experience_2015 experience_2015_sqr experience_justice_2015 experience_justice_2015_sqr"

eststo drop *

/* This section uses data from RAIS, restricted access matched employee-employer data in Brazil. These variables were excluded from the
replication dataset since they cannot be publicly shared, so the section below will not run in the replication exercise. */

eststo: reghdfe FE_IHS_cs $trait work_before, a(connected_sets) vce(robust)
estadd local modelo "Inverse Sine"
estadd local CS "Yes"
qui levelsof(connected_sets) if e(sample)==1
estadd local cluster `r(r)'

eststo: reghdfe FE_IHS_cs $trait log_wage_bef_jud , a(connected_sets) vce(robust)
estadd local modelo "Inverse Sine"
estadd local CS "Yes"
qui levelsof(connected_sets) if e(sample)==1
estadd local cluster `r(r)'

eststo: reghdfe FE_IHS_cs $trait work_before ib5.quintile, a(connected_sets Concurso_enc) vce(robust)
estadd local modelo "Inverse Sine"
estadd local CS "Yes"
qui levelsof(connected_sets) if e(sample)==1
estadd local cluster `r(r)'

eststo: reghdfe FE_IHS_cs $trait work_before if _est_est3==1, a(connected_sets) vce(robust)
estadd local modelo "Inverse Sine"
estadd local CS "Yes"
qui levelsof(connected_sets) if e(sample)==1
estadd local cluster `r(r)'

esttab using "$paper_tables/regressions_traits_expanded.tex", drop(_cons 5.quintile) booktabs f replace ///
	stats(N r2 cluster CS, fmt(%12.0fc %12.3fc) labels(Observations R-Squared "Number Connected Sets" "CS fixed-effect?" )) ///
	label  nomtitles se r2 star(* 0.10 ** 0.05 *** 0.01)  ///
	coeflabel(gender_RAIS "Male" age_2015 "Age in 2015"  age_2015_sqr "Age (squared)" educ_grad "Graduate degree" ///
	experience_2015 "Formal labor experience in 2015" experience_2015_sqr "Formal experience (squared)" ///
	experience_justice_2015 "Formal judicial experience in 2015" experience_justice_2015_sqr "Judicial experience (squared)" ///
	work_before "Formal experience outside judicial sector" 1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile")

******************************************************************************
* Table A12: Correlation between courts' fixed-effects and courts' characteristics
******************************************************************************	

bys connected_sets: egen FE_sd_cs_court = sd(court_fe)
gen FE_IS_court_cs = court_fe / FE_sd_cs_court

global trait_courts3 "capital log_population log_gdp share_urban court_level_2 court_level_3 court_level_5 type_court_short_2 type_court_short_3 type_court_short_4 type_court_short_5 type_court_short_6"

/* This section uses data from RAIS, restricted access matched employee-employer data in Brazil. These variables were excluded from the
replication dataset since they cannot be publicly shared, so the section below will not run in the replication exercise. */

eststo drop *
eststo: reghdfe FE_IS_court_cs $trait_courts3, a(connected_sets) vce(robust)
estadd local modelo "Inverse Sine"
estadd local CS "Yes"
qui levelsof(connected_sets) if e(sample)==1
estadd local cluster `r(r)'

esttab using "$paper_tables/regressions_traits_courts.tex", keep($trait_courts3 )  booktabs f replace ///
	stats(N r2 cluster CS, fmt(%12.0fc %12.3fc) labels(Observations R-Squared "Number Connected Sets" "CS fixed-effect?" )) ///
	refcat(capital "\textit{Judicial district characteristics}" type_court_short_2 "\textit{Type of courts}" , nolabel) ///
	coeflabel(court_level_2 "Second level" court_level_3 "Third level" court_level_5 "Special level" ///
	type_court_short_2 "Criminal court" type_court_short_3 "Civil court" type_court_short_4 "Family court" ///
	type_court_short_5 "Small-stakes court" type_court_short_6 "Other courts" share_urban "Share urban households (2010)") ///
	label  nomtitles se r2 star(* 0.10 ** 0.05 *** 0.01)  

******************************************************************************
********    Figure A3: Histogram of simulated beta-coefficients    ***********
******************************************************************************

preserve

	*** RANDOMIZATION INFERENCE USING QUINTILES ****
	keep Judge_idP YM Court_idP Rank_FE_IS Rank_concurso Concurso_enc  judge_fe_std

	set seed 12345
	loc i = 1
	cap mat drop a
	set matsize 1000
	forv i = 1/1000 {
		gen rand = runiform()
		sort Concurso_enc rand
		by Concurso_enc: gen fake_rank = _n
		by Concurso_enc: egen total_concurso =  max(fake_rank)
		qui gen quintile = .
		qui replace quintile  = 5 if (fake_rank>0.8*total_concurso)
		qui replace quintile  = 4 if (fake_rank>0.6*total_concurso & fake_rank<=0.8*total_concurso)
		qui replace quintile  = 3 if (fake_rank>0.4*total_concurso & fake_rank<=0.6*total_concurso)
		qui replace quintile  = 2 if (fake_rank>0.2*total_concurso & fake_rank<=0.4*total_concurso)
		qui replace quintile  = 1 if (fake_rank<=0.2*total_concurso)
		qui replace quintile = . if fake_rank == .

		qui reg judge_fe_std ib5.quintile i.Concurso_enc, robust
		
		local t = _b[1.quintile]/_se[1.quintile]
		local p_value = 2*ttail(e(df_r),abs(`t'))
		mat a = nullmat(a) \ (_b[1.quintile], `p_value', `i')
		
		drop rand quintile fake_rank total_concurso
		loc ++i
	}

	svmat double a, n(col)
	
	histogram c1, xline(0.225) freq graphregion(color(white)) xtitle("Randomization Inference coefficients") ///
	text(85 .18  "{&beta} = .222", size(medsmall)) ///
	xlabel(-.25(0.05)0.25) ylab(, nogrid) fcolor(dknavy) lcolor(black) lw(medthin)
	graph export "$paper_figures/randomiz_estimates_quintile.pdf", replace

restore

*save "$data_out/judges_estimation_with_FE.dta", replace


******************************************************************************
*** Table A11: Robustness - Case duration and entrance exam performance ******
******************************************************************************

**SAO PAULO ***
gen splog_inv = -splog_fe_std
gen SP_tag = (splog_fe_std != .)

eststo drop *
eststo: reg splog_inv judge_fe_std i.Concurso_enc, robust
eststo: reg judge_fe_std ib5.quintile i.Concurso_enc if SP_tag == 1, robust
eststo: reg splog_inv ib5.quintile i.Concurso_enc if SP_tag == 1, robust

esttab using "$paper_tables/regs_SP_duration.tex", drop(_cons *Concurso_enc* 5.quintile) booktabs  f replace ///
	stats(N r2, fmt(%9.0gc %9.2fc) ///
	labels(Observations R-Squared)) label  se ///
	r2 coeflabels(FE_IHS_std "Judge FE (output)" 1.quintile "Top quintile" 2.quintile "4th quintile" 3.quintile "3rd quintile" ///
	4.quintile "2nd quintile" judge_fe_std "Judge FE (output)") mtitles("Judge FE (duration)" "Judge FE (output)" "Judge FE (duration)") ///
	star(* 0.10 ** 0.05 *** 0.01)

******************************************************************************
*** Table A5: Heterogeneity: entrance exam impact by experience as judge *****
******************************************************************************

gen experiencia_painel = 2012 - Year
gen above_median = experiencia_painel >= 5

eststo drop *

eststo: reg judge_fe_std ib5.quintile i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

eststo: reg judge_fe_std ib5.quintile##above_median i.Concurso_enc, robust
estadd local concurso "Yes"
estadd local modelo "Inverse Sine"
qui levelsof(Concurso_enc) if e(sample)==1
estadd local cluster `r(r)'

esttab using "$paper_tables/regressions_quintiles_het_experience.tex", booktabs  f drop(_cons *Concurso_enc* *5.quintile* *0.above_median*) replace ///
	stats(N r2  cluster concurso ,labels(Observations R-Squared "Number Admission Cohorts" ///
	"Concurso Fixed-Effect" )) label  nomtitles se r2 star(* 0.10 ** 0.05 *** 0.01)  ///
	coeflabels(1.quintile "Top quintile" 2.quintile "4th quintile"  3.quintile "3rd quintile"  ///
	4.quintile "2nd quintile" 1.quintile#1.above_median "Top quintile*Above median experience" ///
	2.quintile#1.above_median "4th quintile*Above median experience" 3.quintile#1.above_median "3rd quintile*Above median experience" ///
	4.quintile#1.above_median "2nd quintile*Above median experience" 1.above_median "Above median experience")
		
	
